package ip.ejb.entity;
/***********************************************************************
 * Module:  UserBean.java
 * Author:  mojsilo
 * Purpose: Defines the Class UserBean
 ***********************************************************************/

import java.util.Collection;

import ip.ejb.sequence.SequenceSessionLocal;
import ip.ejb.sequence.SequenceSessionLocalHome;

import javax.ejb.CreateException;
import javax.ejb.EntityContext;
import javax.naming.InitialContext;

/**
 * @ejb.bean 
 * 	cmp-version="2.x"
 *  name="User"
 *  type="CMP"
 *  jndi-name="ip/ejb/entity/User"
 *  local-jndi-name="ip/ejb/entity/UserLocal"
 *  reentrant="false"
 *  
 * @ejb.finder      
 *   signature="java.util.Collection findAll()"
 *   query="SELECT OBJECT(a) FROM User a"
 *   method-intf="LocalHome"
 *
 * @ejb.finder      
 *   signature="ip.ejb.entity.UserLocal findAdmin(java.lang.String username, java.lang.String password, java.lang.String role)"
 *   query="SELECT OBJECT(a) FROM User a WHERE a.username = ?1 AND a.password= ?2 AND a.role = ?3"
 *   method-intf="LocalHome"
 *
 * @ejb.finder      
 *   signature="ip.ejb.entity.UserLocal findUser(java.lang.String username, java.lang.String password)"
 *   query="SELECT OBJECT(a) FROM User a WHERE a.username = ?1 AND a.password = ?2"
 *   method-intf="LocalHome"
 *      
 * @ejb.finder      
 *   signature="ip.ejb.entity.UserLocal findByUsername(java.lang.String username)"
 *   query="SELECT OBJECT(a) FROM User a where a.username=?1"
 *   method-intf="LocalHome"
 *         
 * @ejb.persistence
 * 	table-name="user"
 * 
 * @ejb.transaction
 * 	type="Required"
 * 
 * @jboss.table-name
 * 	table-name="user"
 * 
 * @jboss.create-table
 * 	create="true"
 * 
 * @jboss.remove-table
 * 	remove="false"  
 */

public abstract class UserBean implements javax.ejb.EntityBean
{
	private EntityContext ctx = null;
	private SequenceSessionLocal sequenceSession = null;

	/**
	 * @ejb.create-method view-type="both"
	 */
	public UserPK ejbCreate(String firstname, String lastname, String username, String password, String address, String jmbg, String role) throws CreateException {

		setUserId(sequenceSession.getNextValue("User"));
		setFirstname(firstname);
		setLastname(lastname);
		setUsername(username);
		setPassword(password);
		setAddress(address);
		setJmbg(jmbg);
		setRole(role);
		return null;
	}   
	
	public void ejbPostCreate(String firstname, String lastname, String username, String password, String address, String jmbg, String role){}
	
	public void setEntityContext(EntityContext ctx) throws javax.ejb.EJBException
	{
		SequenceSessionLocalHome home = null;
		Object obj = null;
		this.ctx = ctx;
		try {
			obj = (new InitialContext()).lookup("ejb/util/SequenceSessionLocal");
			home = (SequenceSessionLocalHome) obj;
			sequenceSession = home.create();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void unsetEntityContext() throws javax.ejb.EJBException
	{
		this.ctx = null;
		try {
			sequenceSession.remove();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @ejb.persistence column-name="user_id"
	 * @ejb.interface-method view-type="both"
	 * @ejb.pk-field
	 * @jboss.jdbc-type type="BIGINT"
	 * @jboss.sql-type  type="BIGINT(20)"
	 */
	public abstract long getUserId();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setUserId(long id);


	/**
	 * @ejb.persistence column-name="firstname"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="VARCHAR"
	 * @jboss.sql-type  type="VARCHAR(30)"
	 */
	public abstract String getFirstname();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setFirstname(String newFirstname);

	/**
	 * @ejb.persistence column-name="lastname"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="VARCHAR"
	 * @jboss.sql-type  type="VARCHAR(30)"
	 */
	public abstract String getLastname();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setLastname(String lastname);

	/**
	 * @ejb.persistence column-name="username"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="VARCHAR"
	 * @jboss.sql-type  type="VARCHAR(10)"
	 */
	public abstract String getUsername();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setUsername(String username);

	/**
	 * @ejb.persistence column-name="password"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="VARCHAR"
	 * @jboss.sql-type  type="VARCHAR(10)"
	 */
	public abstract String getPassword();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setPassword(String password);

	/**
	 * @ejb.persistence column-name="jmbg"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="VARCHAR"
	 * @jboss.sql-type  type="VARCHAR(13)"
	 */
	public abstract String getJmbg();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setJmbg(String jmbg);

	/**
	 * @ejb.persistence column-name="address"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="VARCHAR"
	 * @jboss.sql-type  type="VARCHAR(40)"
	 */
	public abstract String getAddress();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setAddress(String address);

	/**
	 * @ejb.persistence column-name="role"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="VARCHAR"
	 * @jboss.sql-type  type="VARCHAR(1)"
	 */
	public abstract String getRole();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setRole(String role);
	
	/**
	 * @ejb.interface-method view-type="local"
	 * @ejb.relation
	 *   name="Order-User"
	 *   role-name="Order-purchasedby-User"
	 *   target-ejb="Order"
	 */
	public abstract Collection getOrder();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setOrder(Collection orders);
	
	public void ejbActivate() throws javax.ejb.EJBException{}
	public void ejbLoad() throws javax.ejb.EJBException{}
	public void ejbPassivate() throws javax.ejb.EJBException{}
	public void ejbRemove() throws javax.ejb.RemoveException, javax.ejb.EJBException{}
	public void ejbStore() throws javax.ejb.EJBException{}
}